(prompt "Please wait... Loading Farsi.")
(defun rtd (d)
 (setq d (/ (* d 180.0) pi))
)
(defun dtr (d)
 (setq d (/ (* d pi) 180.0))
)
(defun hr (hh rr)
 (setq htd hh rot rr)
 (princ "\nHeight <")
 (princ (rtos htd))
 (princ ">:")
 (setq ht (getdist st))
 (if (= ht nil)
  (setq ht htd)
 )
 (princ "\nRotation angle <")
 (princ (angtos (dtr rot)))
 (princ ">:")
 (setq ro (getangle st))
 (if (= ro nil)
  (setq ro rot)
  (setq rot (setq ro (rtd ro)))
 )
)
(defun *error* (ms)
 (if (= ms "Function cancelled")
     (progn (command) (command))
 )
 (princ "\nerror: ")
 (princ ms)
 (command "ERASE" ss "")
 (setvar "CMDECHO" 1)
 (setvar "HIGHLIGHT" 1)
 (terpri)
)
(defun convert (so)
 (setq chrm1 (list 104 151 152 153 89 229 230 231 84 226 227 228 67 232
	       70 118 193 194 195 99 136 137 138 120 199 200 201 88 233
		  109 166 167 168 98 133 134 135 71 72 32 66 86 77 234))
 (setq chrm2 (list 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
		  48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63
		  64 91 92 93 94 95 96 123 124 125 126 127 128 129))
 (setq chrm (append chrm1 chrm2))
 (setq chrn (append chrm2 (list 32 70 71 72)))
 (setq chro (list (list 68 123) (list 69 124) (list 76 125) (list 79 126)
		  (list 80 127) (list 82 128) (list 84 129) (list 89 130)
		  (list 77 131)
	    )
 )
 (setq chrp (list 68 69 76 77 79 80 82 84 89))
 (setq chrq (list 104 130 129 99 118 120 109 131 98))
  (setq l (strlen so))
  (setq n 1)
  (setq nwo "")
  (while (<= n l)
   (if (= n 1)
    (setq ee " ")
    (setq ee (substr so (1- n) 1))
   )
   (setq f (substr so n 1))
   (if (= n l)
    (setq g " ")
    (setq g (substr so (1+ n) 1))
   )
   (setq asce (ascii ee))
   (setq ascff (setq ascf (ascii f)))
   (setq ascg (ascii g))
   (if (/= (or (and (> ascf 64) (< ascf 91)) (and (> ascf 96) (< ascf 123)))
	nil
       )
    (if (= (member ascf (list 32 65 70 71 72)) nil)
     (progn
      (if (/= (and (> ascf 64) (< ascf 91)) nil)
       (if (/= (member ascf chrp) nil)
	(setq ascf (car (cdr (assoc ascf chro))))
	(setq ascff (setq ascf (+ ascf 32)))
       )
      )
      (if (/= (member asce chrm) nil)
       (if (/= (member ascg chrn) nil)
	(if (= (member ascf chrq) nil)
	 (setq ascf (+ (* (- ascf 97) 3) 132))
	 (setq ascf ascff)
	)
	(setq ascf ascff)
       )
       (if (/= (member ascg chrn) nil)
	(if (= (member ascf chrq) nil)
	 (setq ascf (+ (* (- ascf 97) 3) 131))
	 (setq ascf (+ (* (- ascf 97) 3) 130))
	)
	(setq ascf (+ (* (- ascf 97) 3) 130))
       )
      )
     )
    )
   )
   (setq n (1+ n))
   (setq nwo (strcat nwo (chr ascf)))
  )
  (setq so nwo)
)
(defun C:FARSI ()
 (setvar "CMDECHO" 0)
 (setvar "HIGHLIGHT" 0)
 (setq tz (getvar "TEXTSTYLE"))
 (setq st (list 1 2))
 (setq htd (getvar "textsize"))
 (if (or (= rot nil) (= ro nil))
  (setq rot 180.0 ro 180.0)
 )
 (setq hh htd rr rot)
 (setq ss (ssadd))
 (command "POINT" "0,0")
 (setq entl (entlast))
 (ssadd entl ss)
 (setq rp "Style")
 (while (= rp "Style")
  (initget "Style Align Center Fit Middle Right")
  (setq rp (getpoint "Start point or Align/Center/Fit/Middle/Right/Style:"))
  (if (= rp "Style")
   (progn
    (initget "Farsi Farsis Sade Chapi")
    (setq sty (getkword "\nFarsi/Farsis/Sade/Chapi : "))
    (command "TEXT" "s" (eval sty) "0,0" "" "" "")
   )
  )
 (if (= rp nil)(setq st nil))
 (if (/= st nil)
  (progn
   (if (listp rp)
    (progn
     (setq st rp)
     (hr hh rr)
    )
   )
   (if (= rp "Align")
    (progn
     (setq st (getpoint "\nFirst text line point:"))
     (setq pt2 (getpoint st "\nSecond text line point:"))
    )
   )
   (if (= rp "Center")
    (progn
     (setq st (getpoint "\nCenter point:"))
     (hr hh rr)
    )
   )
   (if (= rp "Fit")
    (progn
     (setq st (getpoint "\nFirst text line point:"))
     (setq pt2 (getpoint st "\nSecond text line point:"))
     (princ "\nHeight <")
     (princ htd)
     (princ ">:")
     (setq ht (getdist st))
     (if (= ht nil)
      (setq ht htd)
     )
    )
   )
   (if (= rp "Middle")
    (progn
     (setq st (getpoint "\nMiddle point:"))
     (hr hh rr)
    )
   )
   (if (= rp "Right")
    (progn
     (setq st (getpoint "\nRight point:"))
     (hr hh rr)
    )
   )
  )
 )
 (setq ro (+ ro 180))
 (princ "\nText:")
 (if (= st nil)
  (command "dtext" "")
  (progn
   (if (listp rp)
    (command "dtext" st ht ro)
   )
   (if (= rp "Align")
    (command "dtext" "a" st pt2)
   )
   (if (= rp "Center")
    (command "dtext" "c" st ht ro)
   )
   (if (= rp "Fit")
    (command "dtext" "f" st pt2 ht)
   )
   (if (= rp "Middle")
    (command "dtext" "m" st ht ro)
   )
   (if (= rp "Right")
    (command "dtext" "r" st ht ro)
   )
  )
 )
 (while (/= (entnext entl) nil)
  (setq txt (entnext entl))
  (setq e (entget txt))
  (setq s (cdr (setq as (assoc 1 e))))
  (setq s (convert s))
  (setq e (subst (cons 1 nwo) as e))
  (entmod e)
  (setq entl txt)
 )
 (command "ERASE" ss "")
 (setvar "HIGHLIGHT" 1)
 (setvar "CMDECHO" 1)
 )
)
